<?php
/**
 * Created by PhpStorm.
 * CeshiUser: kevin
 * Date: 2018/1/3
 * Time: 13:13
 */

namespace app\index\model\pay;
require_once(dirname(__FILE__).'/'."../../extend/alipay/pagepay/service/AlipayTradeService.php");
require_once(dirname(__FILE__).'/'."../../extend/alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php");
require_once(dirname(__FILE__).'/'."../../extend/alipay/pagepay/buildermodel/AlipayTradeQueryContentBuilder.php");

class Alipay extends BasePay{
    /**
     * 配置
     */
    public function config(){
        return [
            //appid
            'appId' => '2018032202425729',
            //商户号
            'sellerId' => '2088921759977422',
            //商户私钥
            'merchantPrivateKey' => 'MIIEpgIBAAKCAQEA4BwE9x2BQKH27LdXRCYIJVF5BjyfzsCRn85XclQRy7/7skXeLzanypn8iaS6n6/ELrDObmxpxFZVzGWgI44w4ctJSnmXNZ8trK/sMkieBQ39UV9TYwGUTKRL/j9h9T2q/abmc7AQ08NK9ZPztHqgYsLkvB/2VS+Q/7yqBts2F9RrH2D2iP1RTm3csSeXGL4aoo5ILQwnJ/UhHO+o04MnpkBDk+ijAQ01SJ2YqCYfmkd+MsDD6YeZPPsL+tCnHVqfa6/nYFPS3JiOHOCVLiLfn7815DdOVDI5dUBvVw1rPWiLCJQXsCheJscrytm9bOC3Pqm2iFevs1mlYHHJo82zJQIDAQABAoIBAQCOu8m9yaDklUM7CJof4UkW24kFG998QtF8wmKXgkvDRQb9R9p2n4zGUMSytaPWyI9eXWVcltfWIFaM07zmaE1hwaNwN3jCwhrI/VKlw/JAreREyz3EtODK32k3CQ9OrnQVERN9YwmUlbFML2rr653yyMX9uy2cm6rx9iskKsGM8p+fqy+MnbKvFmx1o1rc99QxwdfR1w3RTC0O61qRPyXP8vH23y9snx5Kh4DXf4gKXLZh8W18b7UWHj53sETDv20QXNmgn+PBvSWbhNF0cVZO7AUcwoKjg9xGDm2zjtQAnHk1+Go/h9KmZb/KJ5fyzPXZEkut2ST1qJooxFxZQGcBAoGBAP1rw76J0GdekN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YsLkvB/2VS+Q/7yqBts2F9RrH2D2iP1RTm3csSeXGL4aoo5ILQwnJ/UhHO+o04MnpkBDk+ijAQ01SJ2YqCYfmkd+MsDD6YeZPPsL+tCnHVqfa6/nYFPS3JiOHOCVLiLfn7815DdOVDI5dUBvVw1rPWiLCJQXsCheJscrytm9bOC3Pqm2iFevs1mlYHHJo82zJQIDAQAB',
            //签名方式
            'signType'=>'RSA2',
            //编码格式
            'charset' => 'UTF-8',
            //支付宝网关
            'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
            //日志
            'log'=>true,
            //日志文件
            'logFile' => Pay::getPath().'/alipay.log',
        ];
    }

    /**
     * 支付
     */
    public function pay(){
        $config = $this->config();
        //构造支付参数
        $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
        $payRequestBuilder->setBody($this->title);
        $payRequestBuilder->setSubject($this->title);
        $payRequestBuilder->setTotalAmount($this->getMoney());
        $payRequestBuilder->setOutTradeNo($this->orderNumber);
        $aop = new \AlipayTradeService($config);
        //设置url
        $url = $this->getUrl();
        $response = $aop->pagePay($payRequestBuilder, $url['returnUrl'], $url['notifyUrl']);
        //输出表单
        return $response;
    }

    /**
     * 回调页面
     */
    public function back(){
        return $this->result();
    }

    /**
     * 异步通知
     */
    public function notify($data){
        try{
            //转换数据格式（query string->array）
            parse_str($data, $data);
            // 支付宝交易号
            $tradeNo = $data['trade_no'];
            //处理订单
            if($this->complete($tradeNo)){
                return 'success';
            }
        }catch(\Exception $e){
            if($this->debug){
                Pay::fileLog('notify:'.$e->getMessage(), 'debug.log');
            }
        }
        return 'fail';
    }

}